﻿<template>
    <full-screen-dialog :title="titleMap[mode]" v-model="visible" :width="1200" destroy-on-close @closed="$emit('closed')">

        <el-form :model="entityObj" :rules="rules" :disabled="mode == 'Show'" ref="dialogForm" label-width="100px">


            <el-steps :active="1" align-center finish-status="success">
                <el-step title="已下单" description="" />
                <el-step title="待确认" description="" />
            </el-steps>

           <!--补发、退货--->
           <el-row style="margin-top: 18px;" >
                    <el-col :span="6">
                        <el-form-item label-width="120px" label="订单类型:">{{entityObj.purchaseOrderType==1?"补发":"退货"}}</el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label-width="120px" label="单据状态:">{{entityObj.statusDesc}}</el-form-item>
                    </el-col>
                
                    <el-col :span="6">
                        <el-form-item label-width="120px" label="物料发货状态:">{{entityObj.purchaseOrderType==1?entityObj.sendStatusDesc:"无需发货"}}</el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label-width="120px" label="接单金额:">{{entityObj.receiveAmount}}</el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">
                        <el-form-item label-width="120px" label="订单编号:">{{entityObj.purchaseOrderCode}}</el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label-width="120px" label="要货单位:">
                            <el-tooltip placement="top">
                                <template #content>
                                    联系人: {{entityObj.realName}}
                                    <br />
                                    联系电话:{{entityObj.mobile}}
                                    <br />
                                    收货地址:{{entityObj.address}}
                                </template>
                                <span style="color: blue;">{{entityObj.storeName}}</span>
                            </el-tooltip>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label-width="120px" label="下单人:">{{entityObj.createName}}</el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label-width="120px" label="下单时间:">{{entityObj.createTime}}</el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">
                        <el-form-item label-width="120px" label="采购单位:">
                        <el-tooltip placement="top">
                            <template #content>
                                联系人: {{entityObj.purchaseUnityRealName}}
                                <br />
                                联系电话:{{entityObj.purchaseUnityMobile}}
                                <br />
                                联系地址:{{entityObj.purchaseUnityAddress}}
                            </template>
                            <span style="color: blue;">{{entityObj.purchaseUnityName}}</span>
                        </el-tooltip>
                    </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label-width="120px" label="供应商:">
                            <el-tooltip placement="top">
                                <template #content>
                                    联系人: {{entityObj.supplierRealName}}
                                    <br />
                                    联系电话:{{entityObj.supplierMobile}}
                                    <br />
                                    收货地址:{{entityObj.supplierAddress}}
                                </template>
                                <span style="color: blue;">{{entityObj.supplierName}}</span>
                            </el-tooltip>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      
                    </el-col>
                    <el-col :span="6">
                       
                    </el-col>
                </el-row>
            <!-- <el-row>
                <el-col :span="24">
                    <el-form-item label-width="120px" label="下单备注:">
                        {{entityObj.remarks}}
                    </el-form-item>
                </el-col>
            </el-row> -->
            
            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="120px" label="应发货清单:">
                        <el-table :data="entityObj.purchaseOrderItems" border style="width: 100%" show-summary>
                            <el-table-column label="物料编码" min-width="130" prop="materialCode">
                            </el-table-column>
                            <el-table-column label="物料名称" min-width="130" prop="materialName">
                            </el-table-column>
                            <el-table-column label="规格" min-width="130" prop="materialSpecs">
                            </el-table-column>
                            <el-table-column label="物料类别" min-width="130" prop="materialTypeName">
                            </el-table-column>
                            <el-table-column label="单位" min-width="100" prop="unitName" >
                            </el-table-column>
                            <el-table-column label="采购单价" min-width="120" prop="price">
                            </el-table-column>
                            <el-table-column label="申请数量" min-width="120" prop="afterQty">
                            </el-table-column>
                            <!-- <el-table-column label="金额" min-width="120" prop="totalAmount">
                            </el-table-column> -->
                            <el-table-column label="集采下单数量" min-width="150" prop="confirmQty">
                            </el-table-column>
                            <el-table-column label="供应商接单数量" min-width="150" prop="receiveQty">
                            </el-table-column>
                            <el-table-column label="备注" min-width="200" prop="remarks" v-if="entityObj.purchaseOrderType==0"></el-table-column>
                            <el-table-column label="供应商接单备注" min-width="200" prop="receiveRemarks"  v-else-if="entityObj.purchaseOrderType==1 || entityObj.purchaseOrderType==2"></el-table-column>
                        </el-table>
                    </el-form-item>
                
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8" v-if="entityObj.purchaseOrderItems">
					<el-form-item label-width="120px" label="集采确认数量:">
						<el-input v-model="confirmQty"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8" v-if="entityObj.purchaseOrderItems">
					<el-form-item label-width="120px" label="确认备注:">
						<el-input v-model="confirmRemarks"></el-input>
					</el-form-item>
				</el-col>
				
			</el-row>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
            <el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">确 认</el-button>
        </template>
    </full-screen-dialog>
</template>



<script>
import tool from "@/utils/tool";
export default {
    emits: ["success", "closed"],
    data() {
        return {
            mode: "Add",
            titleMap: {
                Add: "新增",
                Edit: "编辑",
                Show: "查看",
            },
            entityObj: {
                purchaseProtocolId: 0,
                purchaseProtocolCode: "",
                purchaseProtocolName: "",
                status: 1,
                crName: "",
                crDate: "",
                supplier: {},
                protocolDate: [],
                purchaseUnityName: "",
                remarks: "",
                protocolFile: "",
                purchaseProtocolItems: [],
            },
            confirmQty:0,
            confirmRemarks:"",
            materialState: false,
            queryList: {},
            visible: false,
            isSaveing: false,
            needSaveFlag: false,
            //验证规则
            rules: {
                purchaseProtocolName: [
                    { required: true, type: "string", message: "请填写" },
                ],
                purchaseUnityName: [
                    { required: true, type: "string", message: "请填写" },
                ],
                supplier: [
                    { required: true, type: "object", message: "请填写" },
                ],
                protocolDate: [
                    { required: true, type: "array", message: "请填写" },
                ],
                crName: [{ required: true, type: "string", message: "请填写" }],
                crDate: [{ required: true, type: "string", message: "请填写" }],
                materialSpecs: [
                    { required: true, type: "string", message: "请填写" },
                ],
                purchaseProtocolItems: [
                    {
                        required: true,
                        type: "array",
                        message: "请填写",
                        trigger: "change",
                    },
                ],
            },
            props: {
                label: "supplierName",
                value: "supplierId",
            },
            materialProps: {
                label: "materialCode",
                value: "materialId",
                items: "materialItems",
            },
            materialTypeTree: [],
            optionsBrand: [],
            optionsUnit: [],
            itemsData: [],
        };
    },
    computed: {
        // itemsData() {
        //     let filtered = this.entityObj.purchaseProtocolItems;
        //     if (this.queryList.materialTypeIds&&this.queryList.materialTypeIds.length>0) {
        //         filtered = filtered.filter(item => {
        //             return this.queryList.materialTypeIds.includes(item.materialTypeId);
        //         });
        //     }
        //     if (this.queryList.materialCode) {
        //         filtered = filtered.filter(item => {
        //             return item.materialCode.includes(this.queryList.materialCode);
        //         });
        //     }
        //     if (this.queryList.materialName) {
        //         filtered = filtered.filter(item => {
        //             return item.materialName.includes(this.queryList.materialName);
        //         });
        //     }
        //     return filtered;
        // },
    },
    methods: {
        //显示=
        open(mode = "Add") {
            this.mode = mode;
            this.visible = true;
            return this;
        },
        //表单提交方法
        submit() {
            
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    try {
                        this.entityObj.purchaseOrderItems[0].confirmQty=this.confirmQty;
                        this.entityObj.purchaseOrderItems[0].confirmRemarks=this.confirmRemarks;
           
                       
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post("purchase-order/confirm",
                                { ...this.entityObj }
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$emit("fatherMethod");
                            this.isSaveing = false;
                            this.visible = false;
                        } else {
                            this.$message.error("操作失败:" + returnValue.msg);
                            this.isSaveing = false;
                        }
                    } catch (error) {
                        this.loading = false;
                        this.isSaveing = false;
                        return false;
                    }
                }
            });
        },
        //表单注入数据
        async setData(data) {
            try {
                var returnValue = await this.$API.auth.GetProcessApi.get(
                    `purchase-order/${data.purchaseOrderId}`,
                    {}
                );
                if (returnValue.status == 200) {
                    this.entityObj = { ...returnValue.response };
                    this.confirmQty=this.entityObj.purchaseOrderItems[0].receiveQty;
                    this.confirmRemarks=this.entityObj.purchaseOrderItems[0].confirmRemarks;
                    //this.entityObj.purchaseOrderItems[0].confirmQty=this.entityObj.purchaseOrderItems[0].receiveQty;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }    
        },
        async initOptions() {
        },
    },
    async mounted() {
        this.entityObj.purchaseUnityName = tool.data.get("USER_INFO").OrgName;
        this.entityObj.crName = tool.data.get("USER_INFO").Name;
        this.entityObj.crDate = tool.formatDate(new Date());
        await this.initOptions();
    },
};
</script>

<style scoped>
.el-form-item{
  margin-bottom: 5px !important;
}
</style>
